package Jasper.service.impl;


import Jasper.dao.IUserDao;
import Jasper.dao.impl.UserDaoImpl;
import Jasper.entity.Page;
import Jasper.entity.Product;
import Jasper.entity.Type;
import Jasper.entity.User;
import Jasper.service.IUserService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName UserServiceImpl
 * @Author jiangjiajia
 * @JDK-Version 1.8
 * @Date 2021/5/20 7:27 pm
 * @Version 1.0
 * @Description 处理servlet发送过来的请求
 **/
public class UserServiceImpl implements IUserService {
    private IUserDao userDao = new UserDaoImpl();

    /**
     * 校验用户名是否存在
     *
     * @param username
     * @return
     */
    @Override
    public String checkUsername(String username) {
        //通过 Dao 查询用户名是否存在
        int count = userDao.checkName(username);
        if (count > 0) {
            return "用户名已存在";
        }
        return "用户名不存在";
    }

    /**
     * 校验用户名和密码是否正确
     *
     * @param username
     * @param password
     */
    @Override
    public Type checkUser(HttpServletRequest request, HttpServletResponse response, String username, String password, String remember) {
        //传递对象进入dao层

        int id = userDao.checkUser(new User(username, password));
        //数据库没有用户返回Type error值
        if (id == 0) {
            return new Type("error");
        } else if (remember != null) {
            //创建 cookie 构造方法传递 key 和 value
            Cookie cookie = new Cookie("id", username);
            //设置 cookie 的有效时间 -- 以秒为单位
            cookie.setMaxAge(60 * 60);
            //在响应中设置 cookie
            response.addCookie(cookie);
        }
        request.getSession().setAttribute("userid", id);
        return new Type("success");
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @Override
    public Type addUser(User user) {
        int count = userDao.addUser(user);
        if (count >= 1) {
            return new Type("success");
        }
        return new Type("error");
    }

    /**
     * 商品分页查询
     *
     * @param currentPage 当前页
     * @return
     */
    @Override
    public Page getPage(int currentPage) {
        Page page = new Page();
        //给当前页赋值
        page.setCurrentPage(currentPage);
        //每页展示的数据量
        page.setPageSize(2);
        //请求地址赋值
        page.setUrl("UserServlet?action=getPage");
        //每页展示的数据集合 --  数据库查询       第一页（1-1）*2,2  limit 0,2
        page.setList(userDao.getList((currentPage - 1) * page.getPageSize(), page.getPageSize()));

        page.setList(userDao.getList((currentPage - 1) * page.getPageSize(), page.getPageSize()));
        //总条数   --  数据库查询
        page.setTotalCount(userDao.getTotalCount());
        return page;
    }


    /**
     * 商品上架
     *
     * @param product
     * @return
     */
    @Override
    public Type addProducr(Product product) {
        int count = userDao.addProduct(product);
        if (count >= 1) {
            return new Type("success");
        }
        return new Type("error");
    }
}



